<?php
	require_once( '../_config/base.inc.php' );

	use \dayscript\Application;
	use \dayscript\util\Debug;
	use \dayscript\util\Benchmark;
	use \dayscript\ErrorManager;
	use \dayscript\Enviroment;
	use \dayscript\loader\adapter\Zend;
	use \dayscript\loader\LoaderManager;
	
	Application::getInstance( BASE_DIR . '_config/tests.inc.php' );
	ErrorManager::setUproarMode( true );
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>Loading Zend Framework Classes</title>
	<link rel="stylesheet" type="text/css" media="all" href="../_resources/styles.css" />
</head>
<body>
<pre>
<b>Zend:</b>
<?php

	echo "\nCargadores Actuales: " . implode( ',', LoaderManager::getLoaderNames( ) );
	echo "\n<b>Clases Actuales:</b>";
	
	$classes = get_declared_classes( );
	foreach( $classes as $key => $value )	{
		if( strpos( $value, 'dayscript' ) !== false || strpos( $value, 'Zend' ) !== false )
			echo "\n - " . $value;
	}
	
	echo "\n\nInstanciando: Zend_Db_Adapter_Pdo_Mysql.";
	try	{
		$error = new Zend_Db_Adapter_Pdo_Mysql( );
	}
	catch( Exception $e )	{
		echo "\n<b>" . get_class( $e ) . ":</b> " . $e->getMessage( );
	}
	
	$computerName = Enviroment::getSERVER( 'COMPUTERNAME' );
	$zendLibrary = ( $computerName == 'DESARROLLO7' ? 'D:\\Desarrollo\\Spine\\_otrosFW\\zend\\' : '/home/ndaza/zend/' );
	// 'DESARROLLO7' == Computador de Nelson ('COMPUTERNAME' -> Solo Windows)
	
	
	echo "\n\nCreando Cargador para framework de Zend en '" . $zendLibrary . "'.";
	$zLoader = new Zend( 'zend', $zendLibrary );
	echo "\n<b>Creado zLoader:</b> " . Debug::dumpVar( $zLoader, true );
	
	$canLoadZend = $zLoader->canLoadClass( 'Zend_Db_Adapter_Pdo_Mysql' );
	echo "\nVerificando carga de librer&iacute;a Zend: " . Debug::dumpVar( $canLoadZend, true );
	
	if( $canLoadZend )	{
		echo "\nAgregando al manejador.";
		LoaderManager::addLoader( $zLoader );
		echo "\nCargadores Actuales: " . implode( ',', LoaderManager::getLoaderNames( ) );
		
		echo "\n\nInstanciando: Zend_Db_Adapter_Pdo_Mysql.";
		$db = new Zend_Db_Adapter_Pdo_Mysql(array(
		    'host'     => 'localhost',
		    'username' => 'root',
		    'password' => '',
		    'dbname'   => 'mysql'
		));
		echo "\n<b>Creado PDO db:</b> " . Debug::dumpVar( $db, true );
		echo "\n<b>Clases Actuales:</b>";
		
		$classes = get_declared_classes( );
		foreach( $classes as $key => $value )	{
			if( strpos( $value, 'dayscript' ) !== false || strpos( $value, 'Zend' ) !== false )
				echo "\n - " . $value;
		}
		
		echo "\n\nIniciando conexi&oacute;n a la base de datos usando Zend_Db_Adapter_Pdo_Mysql.";
		try {
			$db->getConnection( );
		} catch (Zend_Db_Adapter_Exception $e) {
			echo "\n<b>" . get_class( $e ) . ":</b> " . $e->getMessage( );
		} catch (Zend_Exception $e) {
			echo "\n<b>" . get_class( $e ) . ":</b> " . $e->getMessage( );
		}
		
		echo "\n\nInstanciando: Zend_Db_Adapter_Mysqli.";
		$db = new Zend_Db_Adapter_Mysqli(array(
		    'host'     => 'localhost',
		    'username' => 'root',
		    'password' => ( $computerName == 'DESARROLLO7' ? '' : 'krxo4578' ),
		    'dbname'   => 'mysql'
		));
		echo "\n<b>Creado db:</b> " . Debug::dumpVar( $db, true );
		echo "\n<b>Clases Actuales:</b>";
		
		$classes = get_declared_classes( );
		foreach( $classes as $key => $value )	{
			if( strpos( $value, 'dayscript' ) !== false || strpos( $value, 'Zend' ) !== false )
				echo "\n - " . $value;
		}
		
		echo "\n\nIniciando conexi&oacute;n a la base de datos usando Zend_Db_Adapter_Mysqli.";
		try {
			$conn = $db->getConnection( );
		} catch (Zend_Db_Adapter_Exception $e) {
			echo "\n<b>" . get_class( $e ) . ":</b> " . $e->getMessage( );
		} catch (Zend_Exception $e) {
			echo "\n<b>" . get_class( $e ) . ":</b> " . $e->getMessage( );
		}
		echo "\n<b>Conexi&oacute;n:</b> " . Debug::dumpVar( $conn, true );
	
		
		echo "\n\nSeleccionando el primer usuario 'root' de la base de datos.";
		$sql = 'SELECT * FROM user WHERE User = ?';
		$result = $db->fetchRow( $sql, 'root' );
		echo "\n<b>Respuesta:</b> " . Debug::dump( $result, true );
	}
	else	{
		echo "\n\n<font color=\"#FF0000\">Imposible encontrar Zend_Db_Adapter_Pdo_Mysql en '" . $zendLibrary . "'.</font>";
	}
?>


<b>Benchmark:</b><?php echo Benchmark::getInstance( ) ?>
</pre>
</body>
</html>